﻿/*
 *  Author: Louis A. Leskoviansky
 *  Company: Artemis Solutions Group (http://www.artemis-solutions.com)
 */


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace AjaxControlFramework.Compilation
{
    public class JavaScriptSourceElementCollection : IEnumerable
    {
        //------// Properties \\--------------------------------------------\\
        protected List<JavaScriptSourceElement>[] InternalCollection = new List<JavaScriptSourceElement>[100];


        public JavaScriptSourceElement this[int lineNumber, int elementIndex]
        {
            get { return InternalCollection[lineNumber][elementIndex]; }
            set { InternalCollection[lineNumber][elementIndex] = value; }
        }
        //------\\ Properties //--------------------------------------------//



        //------// Constructors \\------------------------------------------\\
        public JavaScriptSourceElementCollection()
        { }
        //------\\ Constructors //------------------------------------------//



        //------// Methods \\-----------------------------------------------\\
        public virtual void Add(int lineNumber, JavaScriptSourceElement sourceElement)
        {
            sourceElement.LineNumber = lineNumber;
            List<JavaScriptSourceElement> sourceElementList = null;

            if (lineNumber - 1 > InternalCollection.Length)
            {
                sourceElementList = new List<JavaScriptSourceElement>();

                // Expand the internal collection by 100 slots.
                List<JavaScriptSourceElement>[] newInternalCollection = new List<JavaScriptSourceElement>[InternalCollection.Length + 100];
                InternalCollection.CopyTo(newInternalCollection, 0);
                InternalCollection = newInternalCollection;
            }
            else
            {
                sourceElementList = InternalCollection.ElementAtOrDefault(lineNumber);

                if (sourceElementList == null)
                {
                    sourceElementList = new List<JavaScriptSourceElement>();
                }
            }

            sourceElementList.Add(sourceElement);

            InternalCollection[lineNumber] = sourceElementList;
        }


        public virtual void Remove(int lineNumber)
        {
            InternalCollection[lineNumber] = null;
        }


        public virtual void Remove(int lineNumber, JavaScriptSourceElement sourceElement)
        {
            List<JavaScriptSourceElement> sourceElementList = InternalCollection.ElementAtOrDefault(lineNumber);

            if (sourceElementList != null)
            {
                sourceElementList.Remove(sourceElement);
            }
        }


        public virtual IEnumerator GetEnumerator()
        {
            return InternalCollection.GetEnumerator();
        }
        //------\\ Methods //-----------------------------------------------//
    }
}